/**
 * File Comment: Ita a GUI
 */
package edu.gatech.oad.project.presentation;
//$codepro.audit.disable
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import edu.gatech.oad.project.networking.clientSessions.AdminSessionClient;
import edu.gatech.oad.project.networking.clientSessions.DoctorSessionClient;
import edu.gatech.oad.project.networking.clientSessions.NurseSessionClient;
import edu.gatech.oad.project.networking.clientSessions.PatientSessionClient;
import edu.gatech.oad.project.operations.users.Doctor;
import edu.gatech.oad.project.operations.users.Nurse;
import edu.gatech.oad.project.operations.users.Patient;
import edu.gatech.oad.project.operations.users.SystemAdmin;

/**
 * This class is a panel that allows the user
 * to interact with the GUI to log in to the
 * system.
 * 
 * @author Group 17
 * @version 1
 */
public class LogIn extends JPanel {
  
  /**
   * Field txtUser: username field
   */
 private JTextField txtUser;
 
 /**
  * Field passwordField: password field
  */
 private JPasswordField passwordField;
 
 /**
  * Field driver: holds the Driver connected to the GUI
  */
 private Driver driver;
 
 /**
  * Field username: username
  */
 private String username;
 
 /**
  * Field password: password
  */
 private char[] password;
 
 /**
  * Field lblSignInto: label
  */
 public JLabel lblSignInto;
 
 /**
  * Field lblUserName: label
  */
 public JLabel lblUserName;
 
 /**
  * Field lblPassword: label
  */
 public JLabel lblPassword;
 
 /**
  * Field lblSignUp: label
  */
 public JLabel lblSignUp;
 
 /**
  * Field btnSignIn: sign in button
  */
 public JButton btnSignIn;
 
 /**
  * Field btnSignUp: sign up button
  */
 public JButton btnSignUp;

 /**
  * Constructor.
  * 
  * @param driver the Driver connected to the GUI
  */
 public LogIn(Driver driver) {
   this.driver = driver;
  GridBagLayout gridBagLayout = new GridBagLayout();
  gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
  gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
  gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
  setLayout(gridBagLayout);
  
  lblSignInto = new JLabel("Sign Into System");
  lblSignInto.setFont(new Font("Lucida Grande", Font.PLAIN, 30));
  GridBagConstraints gbc_lblSignInto = new GridBagConstraints();
  gbc_lblSignInto.insets = new Insets(0, 0, 5, 5);
  gbc_lblSignInto.gridx = 2;
  gbc_lblSignInto.gridy = 1;
  add(lblSignInto, gbc_lblSignInto);
  
  lblUserName = new JLabel("User name:");
  GridBagConstraints gbc_lblUserName = new GridBagConstraints();
  gbc_lblUserName.anchor = GridBagConstraints.EAST;
  gbc_lblUserName.insets = new Insets(0, 0, 5, 5);
  gbc_lblUserName.gridx = 1;
  gbc_lblUserName.gridy = 3;
  add(lblUserName, gbc_lblUserName);
  
  txtUser = new JTextField(10);
  txtUser.addCaretListener(new CaretListener(){
    public void caretUpdate(CaretEvent e){
      username = txtUser.getText();
    }
  });
  GridBagConstraints gbc_txtUser = new GridBagConstraints();
  gbc_txtUser.insets = new Insets(0, 0, 5, 5);
  gbc_txtUser.fill = GridBagConstraints.HORIZONTAL;
  gbc_txtUser.gridx = 2;
  gbc_txtUser.gridy = 3;
  add(txtUser, gbc_txtUser);
  txtUser.setColumns(10);
  
  lblPassword = new JLabel("Password:");
  GridBagConstraints gbc_lblPassword = new GridBagConstraints();
  gbc_lblPassword.anchor = GridBagConstraints.EAST;
  gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
  gbc_lblPassword.gridx = 1;
  gbc_lblPassword.gridy = 4;
  add(lblPassword, gbc_lblPassword);
  
  passwordField = new JPasswordField(10);
  passwordField.addCaretListener(new CaretListener(){
    public void caretUpdate(CaretEvent e){
      password = passwordField.getPassword();
    }
  });
 
  GridBagConstraints gbc_passwordField = new GridBagConstraints();
  gbc_passwordField.insets = new Insets(0, 0, 5, 5);
  gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
  gbc_passwordField.gridx = 2;
  gbc_passwordField.gridy = 4;
  add(passwordField, gbc_passwordField);
  
  btnSignIn = new JButton("Sign In");
  btnSignIn.addActionListener(new ButtonListener1(this));
  GridBagConstraints gbc_btnSignIn = new GridBagConstraints();
  gbc_btnSignIn.insets = new Insets(0, 0, 5, 5);
  gbc_btnSignIn.gridx = 2;
  gbc_btnSignIn.gridy = 5;
  add(btnSignIn, gbc_btnSignIn);
  
  lblSignUp = new JLabel("Not a user yet? Request for membership:");
  GridBagConstraints gbc_lblSignUp = new GridBagConstraints();
  gbc_lblSignUp.gridwidth = 2;
  gbc_lblSignUp.insets = new Insets(0, 0, 5, 5);
  gbc_lblSignUp.gridx = 1;
  gbc_lblSignUp.gridy = 7;
  add(lblSignUp, gbc_lblSignUp);
  
  btnSignUp = new JButton("Sign up");
  btnSignUp.addActionListener(new ButtonListener2(this));
  GridBagConstraints gbc_btnSignUp = new GridBagConstraints();
  gbc_btnSignUp.insets = new Insets(0, 0, 0, 5);
  gbc_btnSignUp.gridx = 2;
  gbc_btnSignUp.gridy = 8;
  add(btnSignUp, gbc_btnSignUp);
 } // end constructor method
 
 /**
  * Get the Driver.
  * 
  * @return the Driver connected to the GUI
  */
 public Driver getDriver()
 {
   return this.driver;
 } // end getDriver method
 
 /**
  * Get the username.
  * 
  * @return the username
  */
 public String getUsername()
 {
     
     return this.username;
 } // end getUsername method
 
 /**
  * Get the password.
  * 
  * @return the password
  */
 public char[] getPassword()
 {
   return this.password;
 } // end getPassword method
 
 /**
  * This private inner class implements ActionListener for btnSignIn.
  * 
  * @author Group 17
  * @version 1
  */
 private class ButtonListener1 implements ActionListener
 {
   /**
    * Field login: the LogIn being used
    */
   private LogIn login;
   
   /**
    * Field driver: holds the Driver connected to the GUI
    */
   private Driver driver;
   
   /**
    * Constructor.
    * 
    * @param login the LogIn being used
    */
   private ButtonListener1(LogIn login)
   {
     this.login = login;
     this.driver = login.getDriver();
   } // end constructor method
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e)
   {
       //System.out.println(this.login);
       //System.out.println(this.login.getUsername());
       //System.out.println(this.login.getPassword());
       //System.out.flush();
       if ((null != this.login.getUsername()) && (null != this.login.getPassword())) {
           String[] signIn = DriverMain.loginToSystem(this.login.getUsername(),new String(this.login.getPassword()));
           if (null == signIn) {
               return;
           }
           if(signIn[0].equals("true")) {
               this.driver.setCurUser(this.login.getUsername());
               int userType = 0;
               if (signIn[2].equals("patient")) {
                   userType = 0;
               }
               else if (signIn[2].equals("nurse")) {
                   userType = 1;
               }
               else if (signIn[2].equals("doctor")) {
                   userType = 2;
               }
               else if (signIn[2].equals("admin")) {
                   userType = 3;
               }
               synchronized(DriverMain.loginData) {
                   DriverMain.loginData[2] = ""; 
               }
               
               switch (userType) {
                   case 0: this.driver.getFrame().setContentPane(new PatientHome(this.driver)); 
                           this.driver.getFrame().pack(); this.driver.setSession(
                                   new PatientSessionClient(DriverMain.server, DriverMain.outBound,                                           
                                           DriverMain.inBound, DriverMain.token, (Patient) DriverMain.user));
                           break;
                   case 1: this.driver.getFrame().setContentPane(new NurseHome(this.driver)); 
                           this.driver.getFrame().pack(); this.driver.setSession(
                                   new NurseSessionClient(DriverMain.server, DriverMain.outBound,                                           
                                           DriverMain.inBound, DriverMain.token, (Nurse) DriverMain.user));
                           break;
                   case 2: this.driver.getFrame().setContentPane(new DoctorHome(this.driver)); 
                           this.driver.getFrame().pack(); this.driver.setSession(
                                   new DoctorSessionClient(DriverMain.server, DriverMain.outBound,                                           
                                           DriverMain.inBound, DriverMain.token, (Doctor) DriverMain.user));
                           break;
                   case 3: this.driver.getFrame().setContentPane(new SysAdminHome(this.driver)); 
                           this.driver.getFrame().pack(); this.driver.setSession(
                                   new AdminSessionClient(DriverMain.server, DriverMain.outBound,                                           
                                           DriverMain.inBound, DriverMain.token, (SystemAdmin) DriverMain.user));
                           break;
               }
        
           }
           else {
               this.login.btnSignIn.setText("Invalid id or password");
           }
       }
       else {
           this.login.btnSignIn.setText("Invalid id or password");
       }
      
   } // end actionPerformed method
 } // end ButtonListener1 class
 
 /**
  * This private inner class implements ActionListener for btnSignUp.
  * 
  * @author Group 17
  * @version 1
  */
 private class ButtonListener2 implements ActionListener
 {
   /**
    * Field login: the LogIn being used
    */
   private LogIn login;
   
   /**
    * Field driver: holds the Driver connected to the GUI
    */
   private Driver driver;
   
   /**
    * Constructor.
    * 
    * @param login the LogIn being used
    */
   private ButtonListener2(LogIn login)
   {
     this.login = login;
     this.driver = login.getDriver();
   } // end constructor class
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e)
   {
     this.driver.getFrame().setContentPane(new SignUp(this.driver));
     this.driver.getFrame().pack();
   } // end actionPerformed method
 } // end ButtonListener2 class
} // end LogIn class